package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constans.BehaviorConstants;
import com.heima.common.constans.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.heima.common.constans.BehaviorConstants.LIKE_BEHAVIOR;
import static com.heima.model.common.enums.AppHttpCodeEnum.SUCCESS;

@Service
@Slf4j
@Transactional
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private CacheService cacheService;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult likeBehavior(LikesBehaviorDto dto) {
        //1.校验参数
        if (dto == null || !checkDtoParam(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断用户是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setArticleId(dto.getArticleId());
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        //3.点赞数据保存到redis
        //点赞行为
        if (dto.getOperation() == 0) {
            //查看是否点过赞，首次点赞才保存数据，否则不保存
            Object obj = cacheService.hGet(LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
            if (obj != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已经点过赞!");
            }
            //未点赞，保存点赞数据。
            cacheService.hPut(LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString(), user.getId().toString());
            updateArticleMess.setAdd(1);
        } else {
            //取消点赞行为
            log.info("取消点赞，删除当前key:{}, {}", dto.getArticleId(), user.getId());
            cacheService.hDelete(LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
            updateArticleMess.setAdd(-1);
        }
        //发送消息，数据聚合
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(updateArticleMess));
        return ResponseResult.okResult(SUCCESS);
    }

    /**
     * 检查参数是否合规
     *
     * @param dto
     * @return
     */
    private boolean checkDtoParam(LikesBehaviorDto dto) {
        if (dto.getType() < 0 || dto.getType() > 2 || dto.getOperation() < 0 || dto.getOperation() > 1) {
            return false;
        }
        return true;
    }
}
